﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace IESN.MyControl
{
    [ToolboxBitmap(@"e:\dms_ico\TRFFC09.ICO")]
    public partial class Alarm : Control
    {
        private IContainer components;
        private float msngBorderWidth;
        private AlarmStatus mStatus;
        private float mTextFontSize;
        private string mtextString;

        //public event StatusChangedEventHandler StatusChanged;
        public event StatusChangedEventHandler StatusChangedEvent;

        public Alarm()
        {
            base.Resize += new EventHandler(this.Alarm_Resize);
            base.Layout += new LayoutEventHandler(this.Alarm_Layout);
            this.mStatus = AlarmStatus.statusGreen;
            this.msngBorderWidth = 1f;
            this.mtextString = "";
            this.mTextFontSize = 10f;
            this.InitializeComponent();
        }

        private void Alarm_Layout(object sender, LayoutEventArgs e)
        {
            string affectedProperty = e.AffectedProperty;
            if (string.Equals(affectedProperty, "Bounds", StringComparison.CurrentCultureIgnoreCase))
            {
                this.Width = this.Height * 1;
            }
            else
            {
                this.Width = this.Height * 1;
            }
        }

        private void Alarm_Resize(object sender, EventArgs e)
        {
            this.Invalidate();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void DrawLight(AlarmStatus LightToDraw, Graphics grfGraphics)
        {
            //Color orangeRed;
            Color orangeRed = new Color();
            int x = (int)Math.Round((double)(this.Size.Width * 0.01));
            int y = (int)Math.Round((double)(this.Size.Height * 0.01));
            int width = (int)Math.Round((double)(this.Size.Width * 0.98));
            switch (LightToDraw)
            {
                case AlarmStatus.statusRed:
                    orangeRed = Color.OrangeRed;
                    break;

                case AlarmStatus.statusYellow:
                    orangeRed = Color.Yellow;
                    break;

                case AlarmStatus.statusGreen:
                    orangeRed = Color.LimeGreen;
                    break;
            }
            Brush brush = new SolidBrush(orangeRed);
            grfGraphics.FillEllipse(brush, x, y, width, width);
            brush.Dispose();
        }

        //[DebuggerStepThrough]
        private void InitializeComponent()
        {
            this.Name = "UserControl1";
            Size size = new Size(0x128, 0x128);
            this.Size = size;
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            Graphics grfGraphics = pe.Graphics;
            this.DrawLight(this.Status, grfGraphics);
            Font font = new Font("Times New Roman", this.TextFontSize, FontStyle.Regular);
            Brush black = Brushes.Black;
            SizeF ef = grfGraphics.MeasureString(this.TextString, font);
            PointF point = new PointF((float)Convert.ToInt16((double)((((double)this.Width) / 2.0) - (ef.Width / 2f))), (float)Convert.ToInt16((double)((((double)this.Height) / 2.0) - (ef.Height / 2f))));
            grfGraphics.DrawString(this.TextString, font, black, point);
            grfGraphics.Dispose();
        }

        public void ResetStatus()
        {
            this.Status = AlarmStatus.statusGreen;
        }

        public bool ShouldSerializeStatus()
        {
            if (this.mStatus == AlarmStatus.statusGreen)
            {
                return false;
            }
            return true;
        }

        [DefaultValue((float)1f), Description("周围边框的宽度")]
        public float BorderWidth
        {
            get
            {
                return this.msngBorderWidth;
            }
            set
            {
                if (this.msngBorderWidth != value)
                {
                    this.msngBorderWidth = value;
                    this.Invalidate();
                }
            }
        }

        [Description("状态（颜色）")]
        public AlarmStatus Status
        {
            get
            {
                return this.mStatus;
            }
            set
            {
                if (this.mStatus != value)
                {
                    this.mStatus = value;
                    if (this.StatusChangedEvent != null)
                    {
                        this.StatusChangedEvent(this.mStatus);
                    }
                    this.Invalidate();
                }
            }
        }

        public float TextFontSize
        {
            get
            {
                return this.mTextFontSize;
            }
            set
            {
                if (this.mTextFontSize != value)
                {
                    this.mTextFontSize = value;
                    this.Invalidate();
                }
            }
        }

        public string TextString
        {
            get
            {
                return this.mtextString;
            }
            set
            {
                if (string.Equals(this.mtextString, value, StringComparison.CurrentCultureIgnoreCase))
                {
                    this.mtextString = value;
                    this.Invalidate();
                }
            }
        }

        public enum AlarmStatus
        {
            statusGreen = 3,
            statusRed = 1,
            statusYellow = 2
        }
        public delegate void StatusChangedEventHandler(AlarmStatus NewStatus);
    } 
}
